<template>
  <div>
    <el-form label-position="top" label-width="80px" :model="form" ref="form" :rules="rules">
      <el-form-item label="试题题干" prop="stem">

        <Editor :stem="form.stem" @changStem="changStem" ref="Editor" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :style="{
          backgroundColor: theme,
          borderColor: theme,
          fontWeight: 'bold'
        }" @click="submitForm" v-loading.fullscreen.lock="fullscreenLoading">保存</el-button>
        <el-button @click="$router.push({ name: 'Question' })">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Editor from "@/views/vadmin/question/components/editor";
import { addQuestion, updateQuestion } from "@/api/vadmin/question/index";
import { mapGetters, mapState } from "vuex";
import variables from "@/assets/styles/variables.scss";
export default {
  name: "newKnowledge",
  components: { Editor },
  props: ["data"],

  data() {
    return {
      // 遮罩层
      fullscreenLoading: false,
      form: {},
      rightAnswer: "",
      // 表单校验
      rules: {
        stem: [
          { required: true, message: "试题题干不能为空", trigger: "blur" },
        ],
      },
    };
  },

  computed: {
    ...mapState(["settings"]),
    variables() {
      return variables;
    },
    theme() {
      return this.$store.state.settings.theme;
    },
  },

  watch: {
    data: {
      handler: function (newValue) {
        this.form = newValue;
      },
      deep: true,
    },
  },

  created() { },
  mounted() { },

  methods: {
    changStem(val) {
      this.form.stem = val;
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.stem = this.$refs["Editor"].getVal();
      this.$refs["form"].validate((valid) => {
        // console.log("题目", this.form);
        this.form.score = 5; //默认每题1分
        this.form.type = 4; //设置题型
        this.form.explain = "无";
        delete this.form["is_right"];
        delete this.form["knowledge"];


        if (this.form.course == "") {
          this.msgError("请选择课程");
        } else {
          if (valid) {
            this.fullscreenLoading = true;
            if (this.form.id != undefined) {
              updateQuestion(this.form).then((response) => {
                this.fullscreenLoading = false;
                this.msgSuccess("修改成功");
              });
            } else {
              addQuestion(this.form).then((response) => {
                this.msgSuccess("新增成功");
                this.fullscreenLoading = false;
                this.reset();
              });


            }
          }
        }
      });
    },
  },
};
</script>

